package com.lighting.common.feign.decoder;

import com.lighting.common.core.vo.BusinessException;
import com.lighting.common.core.vo.Result;
import feign.FeignException;
import feign.Response;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringDecoder;

import java.io.IOException;
import java.lang.reflect.Type;

public class FeignDecoder extends SpringDecoder {

    public FeignDecoder(ObjectFactory<HttpMessageConverters> messageConverters) {
        super(messageConverters);
    }

    @Override
    public Object decode(Response response, Type type) throws IOException, FeignException {
        Result result = (Result) super.decode(response, type);
        if (result.getCode() != 0) {
            throw new BusinessException(result.getCode(), result.getMsg());
        }
        return result;
    }
}